package io.agora.propeller;

import android.content.Context;
import android.content.res.AssetManager;

import com.google.gson.JsonSyntaxException;
import com.inartar.effect.YNBeautyEffect;
import com.inartar.effect.YNCuteEffect;
import com.inartar.effect.YNEffect;
import com.inartar.effect.YNEffectEngine;
import com.inartar.effect.YNLandscapeEffect;
import com.inartar.effect.YNLongLegEffect;
import com.inartar.effect.YNSharpenEffect;
import com.inartar.effect.YNSlenderEffect;
import com.utils.GsonUtil;
import com.ynsdk.track.YNFaceTrack;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class VideoProcessing {

    class EffectItem {
        private String tips;
        private List<String> effects = new ArrayList<String>();

        public String getTips() {
            return this.tips;
        }
        public void setTips(String tips) {
            this.tips = tips;
        }

        public List<String> getEffects() {
            return this.effects;
        }
        public void setEffects(List<String> effects) {
            this.effects = effects;
        }
    }

    YNFaceTrack mTracker = null;
    YNEffectEngine mEngine = null;
    boolean mEnabled = false;

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        Release();
    }

    public VideoProcessing(Context context)
    {
        mTracker = new YNFaceTrack(context, 0);
        mEngine = new YNEffectEngine();
        AssetManager am = context.getAssets();
        String effectPath = "effects/maomi";
        String cfgPath = effectPath + "/params.json";
        try {
            InputStream is = am.open(cfgPath);
            EffectItem effectItem = GsonUtil.parseJsonWithGson(is, EffectItem.class);
            if(effectItem != null && effectItem.getEffects() != null) {
                final List<YNEffect> effects = new ArrayList<YNEffect>();
                for(String effectName : effectItem.getEffects())
                {
                    YNEffect effect = null;
                    if( effectName.equalsIgnoreCase("cute"))
                    {
                        effect = new YNCuteEffect();
                    }
                    else if( effectName.equalsIgnoreCase("beauty"))
                    {
                        effect = new YNBeautyEffect();
                    }
                    else if( effectName.equalsIgnoreCase("landscape"))
                    {
                        effect = new YNLandscapeEffect();
                    }
                    else if(effectName.equalsIgnoreCase("slender"))
                    {
                        effect = new YNSlenderEffect();
                    }
                    else if(effectName.equalsIgnoreCase("sharpen"))
                    {
                        effect = new YNSharpenEffect();
                    }
                    else if(effectName.equalsIgnoreCase("longleg"))
                    {
                        effect = new YNLongLegEffect();
                    }

                    if( effect != null) {
                        effect.setConfigPath(effectPath + "/" + effectName + ".json");
                        effects.add(effect);
                    }
                }
                mEngine.addEffects(effects, true);
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public void Release()
    {
        enable(false);
        if( mTracker != null) {
            mTracker.destory();
            mTracker = null;
        }
        if( mEngine != null){
            mEngine.Release();
            mEngine = null;
        }
    }

    public boolean processVideoFrame(byte[] data, int width, int height, int rotation)
    {
        return true;
    }

    public void enable(boolean enable)
    {
        if( enable != mEnabled)
        {
            mEnabled = enable;
            enable(mEngine.getNativeHandler(), enable);
        }
    }

    private native void enable(long nativeHandler, boolean enable);

    static {
        System.loadLibrary("yn_face_track");
        System.loadLibrary("apm-plugin-videoprocessing");
    }
}
